
<template>
  <div>
    <div class="conditions_bar">
    <el-button @click="generateTable()">生成表格</el-button>
    </div>
    <el-button class="my-confirm" @click="confirmBaby2">提交并查看数据</el-button>
    
    <div class="my-table">
      <el-table :show-header='false' :cell-style='get_my_cell_style' :data='tableData' border size='small' style="width:70%;position:absolute;top:200px;left:100px" 
       :span-method="arraySpanMethod" 
      >
         <el-table-column v-for="i in sum_col" :key="i" :prop='"td"+(i-1)' align='center'
            >
            <template slot-scope="scope">
              <!-- <i class="el-icon-time"></i> -->
              <span v-if='i>源区域_列数 && i<=sum_col-2 && scope.$index>目标区域_行数-1'>
                <el-input v-model="scope.row['td'+(i-1)]"></el-input>
              </span>
              <span v-else-if='i == sum_col-1 && scope.$index>=目标区域_行数'>
                <!-- {{ scope.row['td'+(i-1)] }} -->
                {{getHeji(i,scope)}}
              </span>
              <span v-else-if='i == sum_col && scope.$index>=目标区域_行数'>
                {{getZheSuanBi(i,scope)}}
              </span>
              <span v-else>
                {{scope.row['td'+(i-1)].dictName ||scope.row['td'+(i-1)] }}
              </span>
                
              <!-- <span style="margin-left: 10px">{{ scope.row['td'+(i-1)] }}</span> -->
            </template>
          </el-table-column>

      </el-table>

    </div>

  </div>
</template>
 
<script>
export default {
  name: "test5",
  data() {
    return {
      // driverDimY1List: [],
      // driverDimY2List: [],
      // driverDimY3List: [],
      // driverDimX1List: [],
      // driverDimX2List: [],
      // driverDimX3List: [],
      sum_col: 0,
      sum_row: 0,

      options1_1: [{
        value: 'cc',
        label: 'cc'
      }, {
        value: 'b',
        label: 'b'
      }],
      options1_2: [{
        value: '存款',
        label: '存款'
      }],
      options1_3: [{
        value: '业务一部',
        label: '业务一部'
      }, {
        value: '业务二部',
        label: '业务二部'
      }],
      options2_1: [{
        value: '奖金、津贴',
        label: '奖金、津贴'
      }, {
        value: '职工福利费',
        label: '职工福利费'
      }],
      options2_2: [{
        value: '对公贷款',
        label: '对公贷款'
      },{
        value: '流动资金贷款',
        label: '流动资金贷款'
      }],
      options2_3: [{
        value: '业务三部',
        label: '业务三部'
      }],
      tableData: [
        {},
        {},
        {},
        {},
        {},
        {},
        {},
      ],
      源区域_行数: 0,
      源区域_列数: 0,
      目标区域_行数: 0,
      目标区域_列数: 0,

      arr_a: [],
      arr_b: [],
       

    };
  },
   props:{
    driverDimY1List:{
        type:Array,
        default:()=>[],
    },
    driverDimY2List:{
        type:Array,
        default:()=>[],
    },
    driverDimY3List:{
        type:Array,
        default:()=>[],
    },
    driverDimX1List:{
        type:Array,
        default:()=>[],
    },
    driverDimX2List:{
        type:Array,
        default:()=>[],
    },
    driverDimX3List:{
        type:Array,
        default:()=>[],
    }
  },
  mounted() {

  },
  watch: {
    driverDimY1List(newVal, oldVal) {
      newVal.length == 0 && (this.driverDimY2List = [])
    },
    driverDimY2List(newVal, oldVal) {
      newVal.length == 0 && (this.driverDimY3List = [])
    },
    driverDimX1List(newVal, oldVal) {
      newVal.length == 0 && (this.driverDimX2List = [])
    },
    driverDimX2List(newVal, oldVal) {
      newVal.length == 0 && (this.driverDimX3List = [])
    },
  },
  methods: {
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if(rowIndex < this.目标区域_行数 && columnIndex >= (this.源区域_列数+this.目标区域_列数)) {
        // 合计-折算比
        if(rowIndex == 0 && (columnIndex == this.sum_col-1 || columnIndex == this.sum_col-2)) return [this.目标区域_行数,1]
        else return [0,0]
      }
      
      if(rowIndex < this.目标区域_行数 && columnIndex>=this.源区域_列数 && columnIndex < this.sum_col-2) {
        // 目标
        if(rowIndex == 0) {
          if(((columnIndex - this.源区域_列数) % this.arr_b[0].col) == 0) {
            return [1, this.arr_b[0].col]
          }else {
            return [0,0]
          }
        }
        if(this.arr_b[1] && rowIndex == 1) {
          if(((columnIndex - this.源区域_列数) % this.arr_b[1].col) == 0) {
            return [1, this.arr_b[1].col]
          }else {
            return [0,0]
          }
        }
        if(this.arr_b[2] && rowIndex == 2) {
          if(((columnIndex - this.源区域_列数) % this.arr_b[2].col) == 0) {
            return [1, this.arr_b[2].col]
          }else {
            return [0,0]
          }
        }
      }


      if(columnIndex<this.源区域_列数 && rowIndex >= this.目标区域_行数) {
        // 源
        if(columnIndex == 0) {
          if( (rowIndex - this.目标区域_行数) % this.arr_a[0].row == 0 ) {
            return [this.arr_a[0].row,1]
          }else {
            return [0,0]
          }
        }
        if(this.arr_a[1] && columnIndex == 1) {
          if( (rowIndex - this.目标区域_行数) % this.arr_a[1].row == 0 ) {
            return [this.arr_a[1].row,1]
          }else {
            return [0,0]
          }
        }
        if(this.arr_a[2] && columnIndex == 2) {
          if( (rowIndex - this.目标区域_行数) % this.arr_a[2].row == 0 ) {
            return [this.arr_a[2].row,1]
          }else {
            return [0,0]
          }
        }
      }



      // 其他
      return [1,1]
    },
    get_my_cell_style(row) {
      if(row.columnIndex>=this.sum_col-2) {
        return {
          color: 'red'
        }
      }
    },
    generateTable() {
      console.log('开始生成tableData')
      let source11 = this.driverDimY1List
      let source12 = this.driverDimY2List
      let source13 = this.driverDimY3List
      let source21 = this.driverDimX1List
      let source22 = this.driverDimX2List
      let source23 = this.driverDimX3List

      let a1 = source11.length
      let a2 = source12.length
      let a3 = source13.length
      let b1 = source21.length
      let b2 = source22.length
      let b3 = source23.length

      let row_num = (b3>0?3:b2>0?2:1) + a1 * (a2>0?a2:1) * (a3>0?a3:1)
      let col_num = (a3>0?3:a2>0?2:1) + 2 + b1 * (b2>0?b2:1) * (b3>0?b3:1)
      console.log('总行数-row_num:', row_num)
      console.log('总列数-col_num:', col_num)

      this.源区域_列数 = (a3>0?3:a2>0?2:1)
      this.源区域_行数 = a1 * (a2>0?a2:1) * (a3>0?a3:1)
      this.目标区域_列数 = b1 * (b2>0?b2:1) * (b3>0?b3:1)
      this.目标区域_行数 = (b3>0?3:b2>0?2:1)

      this.sum_col = col_num
      this.sum_row = row_num

      let sum_a = a1 * (a2>0?a2:1) * (a3>0?a3:1)
      let sum_b = b1 * (b2>0?b2:1) * (b3>0?b3:1)
      

      let arr_a = [
        {row: a1==1?sum_a:(a3>0?a3:1) * (a2>0?a2:1), col: 1},
        {row: a2==1?sum_a:(a3>0?a3:1), col: 1},
        {row: a3==1?sum_a:1, col: 1},
      ]
      let arr_b = [
        {col: b1==1?sum_b:(b3>0?b3:1) * (b2>0?b2:1), row: 1},
        {col: b2==1?sum_b:(b3>0?b3:1), row: 1},
        {col: b3==1?sum_b:1, row: 1},
      ]
      a3==0 && arr_a.pop()
      a2==0 && arr_a.pop()
      b3==0 && arr_b.pop()
      b2==0 && arr_b.pop()
      this.arr_a = arr_a
      this.arr_b = arr_b
      console.log('左到右-arr_a',arr_a)
      console.log('上到下-arr_b',arr_b)
      
      this.tableData = new Array()
      for(let i=0;i<row_num;i++) {
        this.tableData[i] = new Array()
        for(let j=0;j<col_num;j++) {
          this.tableData[i][j] = ''
        }
      }
      let tem_2 = a3>0?3:a2>0?2:1
      for(let x1=0;x1<b1;x1++) {
        this.tableData[0][x1*arr_b[0]['col'] + tem_2] = source21[x1]
        for(let x2=0;x2<b2;x2++) {
          this.tableData[1][x2*arr_b[1]['col'] + tem_2 + x1*arr_b[0]['col']] = source22[x2]
          for(let x3=0;x3<b3;x3++) {
            this.tableData[2][x3*arr_b[2]['col'] + tem_2 + x2*arr_b[1]['col'] + x1*arr_b[0]['col']] = source23[x3]
          }
        }
      }
      let x1=0,x2=0,x3=0
      let tem = b3>0?3:b2>0?2:1
      for(x1=0;x1<a1;x1++) {
        this.tableData[x1*arr_a[0]['row'] + tem][0] = source11[x1]
        for(x2=0;x2<a2;x2++) {
          this.tableData[x2*arr_a[1]['row'] + tem + x1*arr_a[0]['row']][1] = source12[x2]
          for(x3=0;x3<a3;x3++) {
            this.tableData[x3*arr_a[2]['row'] + tem + x1*arr_a[0]['row'] + x2*arr_a[1]['row']][2] = source13[x3]
          }
        }
      }

      this.tableData[0][0] = '视图/导入'
      this.tableData[0][col_num-1] = '折算比'
      this.tableData[0][col_num-2] = '合计'
      if(b2>0) {
        this.tableData[1][col_num-1] = '折算比'
        this.tableData[1][col_num-2] = '合计'
      }
      if(b3>0) {
        this.tableData[2][col_num-1] = '折算比'
        this.tableData[2][col_num-2] = '合计'
      }
      let xx = b3>0?3:b2>0?2:1
      let yy = a3>0?3:a2>0?2:1
      // 填充input
      for(let i1=xx,i11=0;i1<row_num;i1++,i11++) {
        for(let i2=yy,i22=0;i2<col_num;i2++,i22++) {
          if(i2 == col_num-2) {
            this.tableData[i1][i2] = '0.000000'  
            // this.tableData[i1][i2] = '合计'  
            continue;
          }
          if(i2 == col_num-1) {
            this.tableData[i1][i2] = '0'  
            // this.tableData[i1][i2] = '折算比'  
            continue;
          }
          this.tableData[i1][i2] = '0.000000'
        }
      }
      


      console.log('装填完成-this.tableData', this.tableData)

      let res = []
      for(let i=0;i<this.tableData.length;i++) {
        let op = {}
        let a = this.tableData[i]
        for(let index=0;index<a.length;index++) {
          op['td'+index] = a[index]
        }
        res.push(op)
      }
      this.tableData = res
      console.log('完成-this.tableData', this.tableData)
    },
    getHeji(i ,scope) {
      let res = 0
      for(let i=this.源区域_列数;i<this.sum_col-2;i++) {
        res += Number(scope.row['td'+i])
      }
      if(res > 1) {
        for(let i=this.源区域_列数;i<this.sum_col-2;i++) {
          scope.row['td'+i] = 0.000000
        }
        this.$notify({
          title: '警告',
          message: '合计总和不能超过1',
          type: 'warning'
        });
        res = 0.000000;
      }
      return res
    },
    confirmBaby() {
      console.log('this.tableData--------' ,this.tableData)
      let niceTableData = []
      for(let i=0;i<this.tableData.length;i++) {
        //每一行
        let row = this.tableData[i]
        niceTableData[i] = []
        let num = Object.keys(row).length
        for(let i2=0;i2<num;i2++) {
          // x:第几列 y:第几行 z:单元格字符串
          if(i2==this.sum_col-2 && i >= this.目标区域_行数) {
           niceTableData[i][i2] = {x:i2,y:i,z: this.getHeji(0,{row: row}) }
           continue
          }
          if(i2==this.sum_col-1 && i >= this.目标区域_行数) {
           niceTableData[i][i2] = {x:i2,y:i,z: this.getZheSuanBi(0,{row: row}) }
           continue
          }
          niceTableData[i][i2] = {x:i2,y:i,z:row['td'+i2]}
        }

      }
      
      console.log('niceTableData', niceTableData)
      alert(JSON.stringify(niceTableData))
    },
    confirmBaby2() {
      console.log('debug..........')
      let res = []
      for(let row = this.目标区域_行数; row < this.sum_row; row++) {
        res.push([])
        let res_row = res[res.length-1]
        for(let col = this.源区域_列数; col < this.sum_col-2; col++) {
          let e = { x: '', y:'', v: this.tableData[row]['td'+col] }
          let tdObj = this.tableData[row]['td'+col]
          for(let i1=0;i1<this.源区域_列数;i1++) {
            e['x'] += this.findValueFromBottomToTop(row, i1) + '@@@'
          }
          e['x'] = e['x'].substr(0, e['x'].length-3)

          for(let i1=0;i1<this.目标区域_行数;i1++) {
            e['y'] += this.findValueFromRightToLeft(i1, col) + '@@@'
          }
          e['y'] = e['y'].substr(0, e['y'].length-3)
          res_row.push(e)
        }

      }
      console.log('res', res)
    },
    getZheSuanBi(i ,scope) {
      let res = 0
      for(let i=this.源区域_列数;i<this.sum_col-2;i++) {
        res += Number(scope.row['td'+i])
      }
      res = res > 0? 1 / res : 0
      return res
    },
    findValueFromBottomToTop(rowIndex, columnIndex) { 
      let res = this.tableData[rowIndex]['td'+columnIndex]
      while(res == '') {
        res = this.tableData[--rowIndex]['td'+columnIndex]
      }
      return res
    },
    findValueFromRightToLeft(rowIndex, columnIndex) { 
      let res = this.tableData[rowIndex]['td'+columnIndex]
      while(res == '') {
        res = this.tableData[rowIndex]['td'+--columnIndex]
      }
      return res
    }
  },
};
</script>
